package com.hdcx.Minfresh.adapter;


import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.activity.GoodListActivity;
import com.hdcx.Minfresh.bean.GoodSortWrapper;
import com.hdcx.Minfresh.holder.ClassifyHolder;
import com.hdcx.Minfresh.url.RequstCode;
import com.hdcx.Minfresh.util.AppUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/9.
 */

public class ClassifyRightAdapter extends RecyclerView.Adapter<ClassifyHolder> {
    FragmentActivity mActivity;
    RecyclerView recyclerView;
    private int item = 0;
    GoodSortWrapper  data;
    //private int count = 0;
    private List<GoodSortWrapper.ListBean.SonBean.SonBean2> itemsBean = new ArrayList<>();

    public ClassifyRightAdapter(FragmentActivity mActivity, RecyclerView rightrecycler) {
        this.mActivity = mActivity;
        recyclerView = rightrecycler;
    }

    @Override
    public ClassifyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        View view;
        if(viewType ==1){
            view= layoutInflater.inflate(R.layout.item_fragment_classify_two_1, parent, false);
        }else{
            view= layoutInflater.inflate(R.layout.item_fragment_classify_two_2, parent, false);
        }
        return  new ClassifyHolder(view,viewType);
    }

    @Override
    public void onBindViewHolder(ClassifyHolder holder, final int position) {
        if(getItemViewType(position)==1){
            holder.tv_name.setText(itemsBean.get(position).getName());
        }else{
            ImageLoader imageLoader = ImageLoader.getInstance();
            DisplayImageOptions options = AppUtil.getNormalImageOptions();
            holder.tv_itemname.setText(itemsBean.get(position).getName());
            imageLoader.displayImage(itemsBean.get(position).getImages(),holder.img_main,options);
            holder.layout_item.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mActivity, GoodListActivity.class);
                    Log.e("ClassifyRightAdapter", "ClassifyRightAdapter--onClick--商品列表"+itemsBean.get(position).getId());
                    intent.putExtra("id",itemsBean.get(position).getId());
                    mActivity.startActivityForResult(intent, RequstCode.GOTOGOODLIST);
                }
            });
        }

    }

    @Override
    public int getItemViewType(int position) {
       return itemsBean.get(position).getType();
    }

    @Override
    public int getItemCount() {
        if(itemsBean==null){
            return 0;
        }
        return itemsBean.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if(itemsBean.get(position).getType()==1){
                    return 3;
                }
                return 1;
            }
        });
    }

    public void setData(GoodSortWrapper response) {
        data = response;
        setCount();
    }


    private void setCount() {
        itemsBean.clear();
        if(data ==null || data.getList()==null || data.getList().get(item)==null ||data.getList().get(item).getSon()==null){
            return;
        }
        data.getList().get(item).getSon().size();
        for(int i = 0,n= data.getList().get(item).getSon().size();i<n;i++){
            if(data.getList().get(item).getSon().get(i).getSon()==null){
                break;
            }
            GoodSortWrapper.ListBean.SonBean.SonBean2 temp = new GoodSortWrapper.ListBean.SonBean.SonBean2();
            temp.setName(data.getList().get(item).getSon().get(i).getName());
            temp.setType(1);
            itemsBean.add(temp);
           for(int j = 0,m= data.getList().get(item).getSon().get(i).getSon().size();j<m;j++){
               itemsBean.add(data.getList().get(item).getSon().get(i).getSon().get(j));
           }
        }
        notifyDataSetChanged();
    }

    public void change(int data) {
        item= data;
        setCount();
    }
}
